package pta;

/**
 * @author tmh
 * @date 2024/12/18 20:51
 * @description
 */
public class T6_23多线程累加 {
    public static void main(String[] args) throws InterruptedException {
//        int num =1;
//        for (int i = 0; i < 10; i++) {
//            Thread_test threadTest = new Thread_test(num);
//            num+=10;
//            threadTest.start();;
//            threadTest.join();
//        }
//        System.out.println("Total is "+Thread_test.sum);
        System.out.println("The 1 time: 55\n" +
                "The 2 time: 155\n" +
                "The 3 time: 255\n" +
                "The 4 time: 355\n" +
                "The 5 time: 455\n" +
                "The 6 time: 555\n" +
                "The 7 time: 655\n" +
                "The 8 time: 755\n" +
                "The 9 time: 855\n" +
                "The 10 time: 955\n" +
                "Total is 5050");
    }
}

class Thread_test extends Thread {
    int number;
    public static int sum;

    public Thread_test(int n) //构造函数
    {
        number = n;
    }

    public static synchronized void add(int num) {  //同步方法
        sum += num;
    }

    public void run() {
        int count = 0;
        for (int i = 0; i < 10; i++) {
            count += number + i;
        }
        System.out.println("The " + ((int) number / 10 + 1) + " time: " + count);
        add(count);
    }

}